var app = getApp();
var times = require('../../../utils/util.js');
var WxParse = require('../../../utils/wxParse/wxParse.js');
Page({
	data: {
		starCount: 0,
		cardCur: 0,
		swiperList: [],
		goods: {},
		dealer: wx.getStorageSync('dealer'),
		opens: 0,
		specsIndex: 0, //当前选择的规格索引
		num: 1, //选择的数量
		timea: '',
		cartnum: '', //购物车数量
	},
	onLoad(options) {
		var $th = this;
		//返回减天数 时间范围
		var time = new Date()
		var getdataw = function(tt, num) {
			var iToDay = tt.getDate();
			var iToMon = tt.getMonth();
			var iToYear = tt.getFullYear();
			var newDay = new Date(iToYear, iToMon, (iToDay + num));
			return newDay
		}
		this.setData({
			timea: times.formatTime(getdataw(time, 1), 'M月D日')
		})
		//end
		//购物长度
		var cartnum = 0;
		var cart = wx.getStorageSync('cart');
		for (var i in cart) {
			cartnum++;
		}
		if (cartnum > 0) {
			this.setData({
				cartnum: cartnum
			});
		}
		//end


		var post = {
			id: options.id
		}
		app.globalAjax(
			'/api/goods/detail',
			"get",
			post,
			function(e) {
				if (e.code == 0) {
					wx.setNavigationBarTitle({
						title: e.goods.goodsName
					});

					if (e.goods.specsList == null) { //无规格商品
						e.goods.specsList = [{
							id: '',
							parentId: '',
							price: e.goods.promotionPrice,
							specsName: ''
						}]
					}

					// WxParse.wxParse('describe', 'html', e.goods.describe, $th, 5);
					e.goods.property.manuFactureDate = $th.dateTimetoDate(e.goods.property.manuFactureDate)
					$th.setData({
						goods: e.goods,
						swiperList: e.goods.goodsPicList
					})
					$th.towerSwiper('swiperList');
					// 初始化towerSwiper 传已有的数组名即可
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})


	},
	setopens() { //展开规格选择
		var s = this.data.opens ? 0 : 1;
		this.setData({
			opens: s
		})
	},
	specSelect(e) { //选择规格
		this.setData({
			specsIndex: e.target.dataset.index
		})
	},
	numadd() { //增加数量
		var num = this.data.num;
		var goods = this.data.goods
		num++;
		if (num > goods.stock) {
			wx.showToast({
				title: '库存不足',
				icon: "none"
			});
			num = goods.stock;
		}
		this.setData({
			num: num
		})
	},
	numlower() { //减少数量
		var num = this.data.num;
		num--;
		if (num < 1) num = 1;
		this.setData({
			num: num
		})
	},
	suborder() { //确认下单
		var that = this.data;
		var total = (that.goods.specsList[that.specsIndex].price * that.num);
		total = parseFloat(total).toFixed(2)
		var orderdata = {
			orderGoodsList: [{
				"goodsId": that.goods.goodsId, //商品ID
				"goodsName": that.goods.goodsName, //商品名称
				"num": that.num, //数量
				"pic": that.goods.picUrl, //图片
				"price": that.goods.specsList[that.specsIndex].price, //价格
				"specsId": that.goods.specsList[that.specsIndex].id, //规格id
				"specsName": that.goods.specsList[that.specsIndex].specsName //规格名称
			}],
			total: total, //合计
			remark: "", //备注
			storeId: 0, //供应商ID
			totalAmount: 0, //总金额
		}
		wx.setStorage({
			key: "orderlist",
			data: orderdata //需要存储的数据
		})
		wx.redirectTo({
			url: '/pages/cart/pay/pay'
		})
		// 
	},
	addcart() { //添加购物车
		var cart = wx.getStorageSync('cart')
		var that = this.data;
		var orderGoodsList = {
			"categoryId": that.goods.categoryId, //分类ID
			"goodsId": that.goods.goodsId, //商品ID
			"goodsName": that.goods.goodsName, //商品名称
			"num": that.num, //数量
			"pic": that.goods.picUrl, //图片
			"price": that.goods.specsList[that.specsIndex].price, //价格
			"specsId": that.goods.specsList[that.specsIndex].id, //规格id
			"specsName": that.goods.specsList[that.specsIndex].specsName //规格名称
		}
		if (!cart) {
			cart = {};
		}
		var sets = cart[that.goods.categoryId + "_" + that.goods.goodsId + "_" + that.goods.specsList[that.specsIndex].id]
		if (sets) {
			sets.num = (sets.num + orderGoodsList.num)
		} else {
			cart[that.goods.categoryId + "_" + that.goods.goodsId + "_" + that.goods.specsList[that.specsIndex].id] =
				orderGoodsList
		}

		wx.setStorage({
			key: "cart",
			data: cart //需要存储的数据
		})
		wx.showToast({
			title: '添加成功',
			icon: "none"
		});
		this.setData({
			opens: 0
		})

		//购物长度
		var cartnum = 0;
		var cart = wx.getStorageSync('cart');
		for (var i in cart) {
			cartnum++;
		}
		if (cartnum > 0) {
			this.setData({
				cartnum: cartnum
			});
		}

		// 
	},
	// cardSwiper
	cardSwiper(e) {
		this.setData({
			cardCur: e.detail.current
		})
	},
	// towerSwiper
	// 初始化towerSwiper
	towerSwiper(name) {
		let list = this.data[name];
		for (let i = 0; i < list.length; i++) {
			list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
			list[i].mLeft = i - parseInt(list.length / 2)
		}
		this.setData({
			swiperList: list
		})
	},
	// towerSwiper触摸开始
	towerStart(e) {
		this.setData({
			towerStart: e.touches[0].pageX
		})
	},
	// towerSwiper计算方向
	towerMove(e) {
		this.setData({
			direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
		})
	},
	// towerSwiper计算滚动
	towerEnd(e) {
		let direction = this.data.direction;
		let list = this.data.swiperList;
		if (direction == 'right') {
			let mLeft = list[0].mLeft;
			let zIndex = list[0].zIndex;
			for (let i = 1; i < list.length; i++) {
				list[i - 1].mLeft = list[i].mLeft
				list[i - 1].zIndex = list[i].zIndex
			}
			list[list.length - 1].mLeft = mLeft;
			list[list.length - 1].zIndex = zIndex;
			this.setData({
				swiperList: list
			})
		} else {
			let mLeft = list[list.length - 1].mLeft;
			let zIndex = list[list.length - 1].zIndex;
			for (let i = list.length - 1; i > 0; i--) {
				list[i].mLeft = list[i - 1].mLeft
				list[i].zIndex = list[i - 1].zIndex
			}
			list[0].mLeft = mLeft;
			list[0].zIndex = zIndex;
			this.setData({
				swiperList: list
			})
		}
	},
	dateTimetoDate: function(str) {
		if (!str || str == "") return
		var str = str.split(" ")
		return str[0]
	},
	onShareAppMessage: function(ops) {
		var $th = this;
		if (ops.from === 'button') {

		}
		return {
			title: "[优选]" + $th.data.goods.goodsName,
			path: "/pages/classify/productart/productart?id=" + $th.data.goods.goodsId,
			success: function(res) {
				// 转发成功
				console.log("转发成功:" + JSON.stringify(res));

			},
			fail: function(res) {
				// 转发失败
				console.log("转发失败:" + JSON.stringify(res));
			}
		}
	}
})
